import axios from 'axios';
import { ElMessage } from 'element-plus';
import router from '../router';

const base = axios.create({
    baseURL:'https://q5b2251925.51vip.biz/library/api',
    timeout: 50000
})

base.interceptors.request.use(config=>{
    config.headers.token = localStorage.getItem('token');
    config.headers.AuthKey = localStorage.getItem('AuthKey');
    return config;
},error=>{
    return Promise.reject(error);
})

base.interceptors.response.use(response=>{
    if(response.data.code === 401){
        localStorage.removeItem('token')
        localStorage.removeItem('AuthKey')
        ElMessage.error('身份已过期,请重新登录');
        router.push('/login')
    }
    return response;
},error=>{
    return error;
})

export default base;